<template lang="html">
  <section>
    <el-form :inline='true' class="forms" label-position='right' label-width="100px">
      <el-form-item
        class='forms_item'
        :label='item.label + ":"'
        v-for='item in formdata'
        :key='item.label'
      >
      <!-- input -->
        <el-input
          v-if='item.type === "input"'
          v-model='item.modelData'
          type = ''
          placeholder='请输入'
          style='width : 160px'
          clearable
          size='mini'
        ></el-input>
        <el-input
          v-if='item.type === "textarea"'
          v-model='item.modelData'
          type = 'textarea'
          placeholder='请输入'
          style='width : 700px'
          clearable
          size='mini'
        ></el-input>
        <!-- select -->
        <el-select
          size='mini'
          v-else-if='item.type === "select"'
          v-model='item.modelData'
          style='width : 160px'
          clearable
        >
          <el-option
            v-for='elem in item.options'
            :value='elem.code'
            :key='elem.code'
            :label='elem.label'
          ></el-option>
        </el-select>
        <!-- 日期 -->
        <el-date-picker
          v-else-if='item.type ==="date"'
          v-model="item.modelData"
          value-format='yyyy-MM-dd'
          align="right"
          type="date"
          size='mini'
          style='width:160px;'
          placeholder="选择日期"
          >
        </el-date-picker>
        <!-- 日期范围 start -->
        <el-date-picker
          v-if="item.type === 'daterange'"
          v-model="item.modelData[0]"
          align="right"
          type="date"
          size='mini'
          style='width:180px;'
          placeholder="开始日期"
          value-format='yyyy-MM-dd'
          :picker-options='pickerBegin[item.code].before'>
        </el-date-picker>
        <el-form-item v-if="item.type === 'daterange'" label="至" label-width="40px">
          <el-date-picker
            v-model="item.modelData[1]"
            align="right"
            type="date"
            size='mini'
            style='width:180px;'
            placeholder="结束日期"
            value-format='yyyy-MM-dd'
            :picker-options='pickerBegin[item.code].after'>
          </el-date-picker>
        </el-form-item>
        <!-- 日期范围 end -->
      </el-form-item>
      <div class="button">
        <el-button
          type='primary'
          size="small"
        >立案号提取</el-button>
        <el-button
          type='primary'
          size="small"
          @click='search()'
        >搜索</el-button>
        <el-button
          type='danger'
          size='small'
          @click="clear"
        >清空</el-button>
      </div>
      <!-- <slot></slot> -->
    </el-form>
  </section>
</template>

<script>
export default {
  props : {
    formData : Array,
  },
  data(){
    return {
      formdata: this.formData,
      pickerBegin: {}
    }
  },
  created() {
    this.formdata.forEach((item) => {
      if(item.type === 'daterange'){
        this.pickerBegin[item.code] = {
          before:{
            disabledDate: (time) => {
              let beginDateVal = new Date(item.modelData[1]).getTime();
              if (beginDateVal) {
                return time.getTime() > beginDateVal;
              }
            }
          },
          after:{
            disabledDate: (time) => {
              let beginDateVal = new Date(item.modelData[0]).getTime();
              if (beginDateVal) {
                return time.getTime() < beginDateVal;
              }
            }
          }
        }
      }
    });
  },
  methods : {
    search(){
      let param = {};
      this.formdata.forEach((elem) => {
        param[elem.code] = elem.modelData;
      })
      this.$emit('do-search',param);
    },
    clear() {
      let newFormData = [];
      this.formdata.forEach((elem) => {
        elem.modelData = '';
        newFormData.push(elem);
      });
      this.formdata = [];
      newFormData.forEach((elem) => {
        this.formdata.push(elem);
      });
      this.search();
    }
  }
}
</script>

<style lang="scss">
  .forms {
    width: 100%;
    padding: 0 12px;
    .forms_item {
      margin-bottom: 20px;
    }
  }
  .button {
    width: 230px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
  }
</style>
